package com.spring.xiatian.user.controller.api;

import com.spring.xiatian.enums.ResultEnum;
import com.spring.xiatian.exception.AppException;
import com.spring.xiatian.user.entity.SystemUser;
import com.spring.xiatian.user.service.SystemUserService;
import com.spring.xiatian.user.service.feign.OrderServiceFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Created by xiaoxia on 2017/11/30 0030 13:37.
 */
@RestController
@RequestMapping("/api")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    SystemUserService systemUserService;

    @Autowired
    OrderServiceFeign orderServiceFeign;

    @RequestMapping("/user")
    public SystemUser findUserById(@Valid SystemUser user, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            logger.info("调用 scb-order 出错--->error={}",msg);
            throw new AppException(ResultEnum.UserEnums.PARAM_ERROR.getCode(),msg);
        }
        return systemUserService.findSystemUserById(user.getId());
    }

    @RequestMapping("/list")
    public List<SystemUser> queryUserList(){
        return systemUserService.queryUserList();
    }

    @RequestMapping("/getOrderList")
    public String getOrderList(){
        String response = orderServiceFeign.getOrderList();
        logger.info("调用 scb-order--->response={}",response);
        return response;
    }

}
